package com.mzx.wechat321.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mzx.wechat321.common.ResponseCommon;
import com.mzx.wechat321.pojo.MsgCodeKey;
import com.mzx.wechat321.pojo.Teml;
import com.mzx.wechat321.pojo.ReplyMessage;
import com.mzx.wechat321.pojo.UserRqPojo;
import com.mzx.wechat321.service.MsgService;
import com.mzx.wechat321.utill.JsoupUtill;
import com.mzx.wechat321.utill.WechatMessageUtil;
import com.mzx.wechat321.utill.MyUtill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.mzx.wechat321.utill.ByteToUtill.byteToStr;

@Controller
public class MyController {

    @ResponseBody
    @RequestMapping({"/","/index"})
    public String index(){
        return "放过我QAQ，玛卡吧卡";
    }


    // 与接口配置信息中的Token要一致
    private static String token = "mzxtoken";

    @Autowired
    MsgService msgService;

    @RequestMapping(value = "/getCont",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getCont(String search)  throws IOException {
        System.out.println(search);
        JSONObject jsonObject = JSON.parseObject(JsoupUtill.toGet(search));
        Object temls = JSON.parseObject(jsonObject.toJSONString(),Teml.class);
        System.out.println(temls);
        return JsoupUtill.toGet(search);
    }

    @RequestMapping(value = "/getToken",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getToken(HttpServletRequest request) throws IOException {

        ReplyMessage replyMessage = null;
        String msg = "感谢使用，本次查询结果为：\n";
        // 回复内容
        String responseMessage = "success";
        try{
            // token请求验证
            if (request.getParameter("echostr")!=null){
                return MyUtill.getTestToken(request);
            }
            replyMessage = MyUtill.getReplyMessage(request);
            replyMessage.setCreateTime(System.currentTimeMillis());
            System.out.println(replyMessage);
            // 对消息进行处理
            switch (replyMessage.getMsgType()){
                case WechatMessageUtil.MESSAGE_TEXT:
                    if (Character.isDigit(replyMessage.getContent().charAt(0))){
                        if (replyMessage.getContent().length()>3 && !replyMessage.getContent().contains("http")){
                            msg += msgService.searchMsgbyCode(replyMessage.getContent()).getMsg_content();
                        }else {
                            if (replyMessage.getContent().contains("http")){
                                throw new NullPointerException("无内容报错返回");
                            }
                            msg += MyUtill.getCont(replyMessage.getContent());
                        }
                    }else {
                        // 关键字查询暂缓
                        List<MsgCodeKey> msgcok = msgService.searchMsgbyKey(replyMessage.getContent());
                        for (MsgCodeKey m:msgcok){
                            System.out.println(m);
                            msg += m.getMsg_content() + "\n";
                        }
                        if (replyMessage.getContent().contains("http") || msgcok.size() < 1){
                            throw new NullPointerException("无内容报错返回");
                        }
                    }

                    replyMessage.setContent(msg);
                    responseMessage = WechatMessageUtil.textMessageToXml(replyMessage).replace(" ","");
                    break;
                case WechatMessageUtil.MESSAtGE_IMAGE:
                    if(replyMessage.getCreateTime()!=0){
                        throw new NullPointerException("无内容报错返回");
                    }
                    break;
                case WechatMessageUtil.MESSAGE_VOICE:
                    System.out.println("vocie");
                    break;
                case WechatMessageUtil.MESSAGE_LOCATION:
                    System.out.println("坐标");
                    break;
                case WechatMessageUtil.MESSAGE_EVENT_SUBSCRIBE:
                    System.out.println("订阅");
                    break;
                case WechatMessageUtil.MESSAGE_EVENT_UNSUBSCRIBE:
                    System.out.println("取消");
                    break;
                case WechatMessageUtil.MESSAGE_LINK:
                    System.out.println("link");
                    break;
            }
        }catch (Exception e){
            assert replyMessage != null;
            replyMessage.setContent("本次请求异常，请检查数据重新尝试");
            responseMessage = WechatMessageUtil.textMessageToXml(replyMessage);
        }


        return responseMessage;
    }

}
